package com.aliyun.heiyu.powermaster.server;

import com.aliyun.heiyu.powermaster.server.agent.Agent;
import com.aliyun.heiyu.powermaster.server.agent.AgentManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CapExecutor implements IStrategyExecutor {
    private static Logger log = LoggerFactory.getLogger(CapExecutor.class);

    private int strength;
    private AgentManager agentManager;

    public CapExecutor(AgentManager am) {
        this.agentManager = am;
    }

    @Override
    public void setStrength(int strength) {
        if (strength < this.strength) {
            for (Agent agent : agentManager.getAll("")) {
                agent.cap(agent.getMaxPower(), agent.getMaxFreq());
            }
        }

        this.strength = strength;
    }

    public void onAgentRegister(Agent agent) {
    }

    public void onAgentReport(Agent agent) {
        int capPower = agent.getMaxPower() * (100 - strength) / 100;
        long capFreq = agent.getMaxFreq() * (100 - strength) / 100;

        if (agent.getPower() > capPower || agent.getFreq() > capFreq) {
            agent.cap(capPower, capFreq);
        }
    }

    public void onAgentRemove(Agent agent) {
    }
}
